package com.example.yichuangyun.utils.treelist;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;

import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.utils.producttreelist.ProductNodeModel;

import java.util.ArrayList;
import java.util.List;

public abstract class TreeRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected Context mContext;
    /**
     * 存储所有可见的Node
     */
    protected List<Node> mNodes;
    protected LayoutInflater mInflater;

    /**
     * 存储所有的Node
     */
    protected List<Node> mAllNodes;

    /**
     * 默认不展开
     */
    private int defaultExpandLevel;

    /**
     * @param defaultExpandLevel 默认展开几级树
     */
    public TreeRecyclerAdapter(Context context, List<Node> data, int defaultExpandLevel) {
        this.defaultExpandLevel = defaultExpandLevel;
        mContext = context;
        // 对所有的Node进行排序
        mAllNodes = TreeHelper.getSortedNodes(data, defaultExpandLevel);
        // 过滤出可见的Node
        mNodes = TreeHelper.filterVisibleNode(mAllNodes);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        Node node = mNodes.get(position);
        // 设置内边距
        holder.itemView.setPadding(node.getLevel() * 60, 3, 3, 3);
        // 设置节点点击时，可以展开以及关闭,将事件继续往外公布
        holder.itemView.setOnClickListener(v -> expandOrCollapse(position));
        onBindViewHolder(node, holder, position);
    }

    @Override
    public int getItemCount() {
        return mNodes.size();
    }

    /**
     * 清除掉之前数据并刷新，重新添加
     */
    public void addDataAll(List<Node> mList) {
        mAllNodes.clear();
        addData(-1, mList, defaultExpandLevel);
    }

    /**
     * 清除掉之前数据并刷新  重新添加
     *
     * @param defaultExpandLevel 默认展开几级列表
     */
    public void addDataAll(List<Node> mList, int defaultExpandLevel) {
        mAllNodes.clear();
        addData(-1, mList, defaultExpandLevel);
    }

    /**
     * 在指定位置添加数据并刷新 可指定刷新后显示层级
     *
     * @param defaultExpandLevel 默认展开几级列表
     */
    public void addData(int index, List<Node> mList, int defaultExpandLevel) {
        this.defaultExpandLevel = defaultExpandLevel;
        notifyData(index, mList);
    }

    /**
     * 在指定位置添加数据并刷新
     */
    public void addData(int index, List<Node> mList) {
        notifyData(index, mList);
    }

    /**
     * 添加数据并刷新
     */
    public void addData(List<Node> mList) {
        addData(mList, defaultExpandLevel);
    }

    /**
     * 添加数据并刷新 可指定刷新后显示层级
     */
    public void addData(List<Node> mList, int defaultExpandLevel) {
        this.defaultExpandLevel = defaultExpandLevel;
        notifyData(-1, mList);
    }

    /**
     * 添加数据并刷新
     */
    public void addData(Node node) {
        addData(node, defaultExpandLevel);
    }

    /**
     * 添加数据并刷新 可指定刷新后显示层级
     */
    public void addData(Node node, int defaultExpandLevel) {
        List<Node> nodes = new ArrayList<>();
        nodes.add(node);
        this.defaultExpandLevel = defaultExpandLevel;
        notifyData(-1, nodes);
    }

    /**
     * 移除node
     */
    public void removeData(Node node) {
        if (node == null) {
            return;
        }
        removeDeleteNode(node);
        for (Node n : mAllNodes) {
            n.getChildren().clear();
        }
        mAllNodes = TreeHelper.getSortedNodes(mAllNodes, defaultExpandLevel);
        mNodes = TreeHelper.filterVisibleNode(mAllNodes);
        notifyDataSetChanged();
    }

    /**
     * 批量移除node
     */
    public void removeData(List<Node> nodes) {
        if (nodes == null || nodes.isEmpty()) {
            return;
        }
        for (Node node : nodes) {
            removeDeleteNode(node);
        }
        for (Node n : mAllNodes) {
            n.getChildren().clear();
        }
        mAllNodes = TreeHelper.getSortedNodes(mAllNodes, defaultExpandLevel);
        mNodes = TreeHelper.filterVisibleNode(mAllNodes);
        notifyDataSetChanged();
    }

    private void removeDeleteNode(Node node) {
        if (node == null) {
            return;
        }
        List<Node> childrens = node.getChildren();
        if (childrens != null && !childrens.isEmpty()) {
            for (Node n : childrens) {
                removeDeleteNode(n);
            }
        }
        mAllNodes.remove(node);
    }

    /**
     * 刷新数据
     */
    private void notifyData(int index, List<Node> mListNodes) {
        for (int i = 0; i < mAllNodes.size(); i++) {
            Node node = mAllNodes.get(i);
            node.getChildren().clear();
            node.isNewAdd = false;
        }
        if (index != -1) {
            mAllNodes.addAll(index, mListNodes);
        } else {
            mAllNodes.addAll(mListNodes);
        }
        // 对所有的Node进行排序
        mAllNodes = TreeHelper.getSortedNodes(mAllNodes, defaultExpandLevel);
        // 过滤出可见的Node
        mNodes = TreeHelper.filterVisibleNode(mAllNodes);
        notifyDataSetChanged();
    }

    /**
     * 获取排序后所有节点
     */
    public List<Node> getAllNodes() {
        if (mAllNodes == null)
            mAllNodes = new ArrayList<>();
        return mAllNodes;
    }

    /**
     * 相应ListView的点击事件 展开或关闭某节点
     */
    public void expandOrCollapse(int position) {
        Node n = mNodes.get(position);
        if (n != null) { // 排除传入参数错误异常
            if (!n.isLeaf()) {
                n.setExpand(!n.isExpand());
                mNodes = TreeHelper.filterVisibleNode(mAllNodes);
                notifyDataSetChanged();
            }
        }
    }

    /**
     * 设置多选
     */
//    public void setChecked(final Node node, boolean checked) {
//        node.setChecked(checked);
//        setChildChecked(node, checked);
//        if (node.getParent() != null)
//            setNodeParentChecked(node.getParent(), checked);
//        notifyDataSetChanged();
//    }

    /**
     * 设置多选
     */
    public void setChecked(final Node node, boolean checked) {
        // 正常逻辑
//        node.setChecked(checked);
//        setChildChecked(node, checked);
//        if (node.getParent() != null)
//            setNodeParentChecked(node.getParent(), checked);
//        notifyDataSetChanged();

        // ------------------------------------------------------------ //
        node.setChecked(checked);
//        if (!checked) {
//            setChildChecked(node, false);
//        }
//        if (node.getParent() != null) {
//            setNodeParentChecked(node.getParent(), checked);
//        }
        notifyDataSetChanged();
        // ------------------------------------------------------------ //

    }
    /**
     * 设置单选
     */
    public void setSingleChecked(final int position) {
        mAllNodes = getAllNodes();
        for (int i = 0; i < mAllNodes.size(); i++) {
            getAllNodes().get(i).setChecked(position == i);
        }
        notifyDataSetChanged();
    }


    /**
     * 设置全部不选中
     */
    public void setAllNoChecked() {
        mAllNodes = getAllNodes();
        for (int i = 0; i < mAllNodes.size(); i++) {
            getAllNodes().get(i).setChecked(false);
        }
    }

    /**
     * 设置是否选中
     */
    public void setChildChecked(Node node, boolean checked) {
        if (!node.isLeaf()) {
            node.setChecked(checked);
            for (Node childrenNode : node.getChildren()) {
                setChildChecked(childrenNode, checked);
            }
        } else {
            node.setChecked(checked);
        }
    }

    private void setNodeParentChecked(Node node, boolean checked) {
        if (checked) {
            node.setChecked(true);
        } else {
            List<Node> mChildren = node.getChildren();
            boolean isChecked = false;
            for (Node children : mChildren) {
                if (children.isChecked()) {
                    isChecked = true;
                }
            }
            //如果所有自节点都没有被选中 父节点也不选中
            if (!isChecked) {
                node.setChecked(false);
            }
        }
        if (node.getParent() != null)
            setNodeParentChecked(node.getParent(), checked);
    }

    public abstract void onBindViewHolder(Node node, RecyclerView.ViewHolder holder, final int position);
}
